package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.LabelValue;
import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.common.util.DateTimeUtil;
import cn.edu.sdyu.ie.crs.domain.Assignment;
import cn.edu.sdyu.ie.crs.service.AssignmentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.List;

@Tag(name = "分配管理", description = "分配信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/assignment")
public class AssignmentController {
    @Autowired
    private AssignmentService assignmentService;

    /**
     * 添加新分配信息
     *
     * @param assignment
     * @return
     */
    @Operation(tags = "分配管理", summary = "添加分配信息")
    @PostMapping("")
    public Result<Void> addAssignment(Assignment assignment) {

        if (assignment.getId() != null && !assignment.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。");
            return Result.failure();
        }
        assignmentService.saveAssignment(assignment);
        return Result.success();
    }

    /**
     * 更新分配信息
     *
     * @param assignment
     * @return
     */
    @Operation(tags = "分配管理", summary = "修改分配信息")
    @PutMapping("")
    public Result<Void> updateAssignment(Assignment assignment) {

        if (assignment.getId() == null || assignment.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        assignmentService.saveAssignment(assignment);
        return Result.success();
    }

    /**
     * 删除指定ID的分配信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "分配管理", summary = "删除分配信息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteAssignmentById(@PathVariable Integer id) {
        assignmentService.deleteAssignmentById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的分配信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "分配管理", summary = "查询指定ID的分配信息")
//    @Parameters(value = {
//            @Parameter(description = "教室ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分配ID")
    })
    @GetMapping("/{id}")
    public Result<Assignment> findAssignmentById(/*@Parameter(description = "教室ID")*/ @PathVariable Integer id) {
        Assignment assignment = assignmentService.findAssignmentById(id);

        if (assignment == null) {
            return Result.failure();
        }

        return Result.success(assignment);
    }

    /**
     * 分页查询分配信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "分配管理", summary = "分页查询分配信息")
    @GetMapping("")
    public Result<PageVO<Assignment>> listAssignmentsPage(@RequestParam(defaultValue = "1") Integer page,
                                                          @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Assignment> list = assignmentService.findByPage(page - 1, pageSize);
        PageVO<Assignment> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }

    /**
     * 统计本周分配执行情况
     */
    @Operation(tags = "分配管理", summary = "统计本周分配执行情况")
    @GetMapping("/count/week")
    public Result<List<LabelValue>> countAssignmentsInWeek() {

        List<LocalDate> dates = DateTimeUtil.getDaysOfWeek();
        List<LabelValue> countList = assignmentService.countGroupByExecutionDateBetween(dates.get(0), dates.get(6));

        return Result.success(countList);
    }

    @GetMapping("/user")
    public Result<List<Assignment>> findAssignmentsByUser(@RequestParam String user) {
        List<Assignment> list = assignmentService.findAvailableAssignments(user);
        return Result.success(list);
    }
}
